<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <meta name="_ctx" th:content="@{/}" />
<link rel="stylesheet" th:href="@{/bootstrap/dist/css/bootstrap.min.css}">
<link rel="stylesheet" th:href="@{/css/login.css}">
<title>首页</title>
<style>
</style>
</head>
<body>
<div class="form-bg">
<div class="container">
    <div class="row">
        <div class="col-md-offset-3 col-md-6">
            <form class="form-horizontal" action="/user/login" th:action="@{/user/login}"  method="post">
                <span class="heading">用户登录</span>
                <div class="form-group">
                    <input type="text" class="form-control" id="username" name="username" placeholder="用户名或电子邮件">
                    <i class="glyphicon glyphicon-user"></i>
                </div>
                <div class="form-group help">
                    <input type="password" class="form-control" id="password" name="password" placeholder="密　码">
                    <i class="glyphicon glyphicon-lock"></i>
                    <a href="#" class="fa fa-question-circle"></a>
                </div>
                <div class="form-group">
                	<p id="error-msg" style="color: red;"></p>
                </div>
                <div class="form-group">
                    <div class="main-checkbox">
                        <input type="checkbox" value="None" id="checkbox1" name="check"/>
                        <label for="checkbox1"></label>
                    </div>
                    <span class="text">Remember me</span>
                    <button id="submit" class="btn btn-default">登录</button>
                </div>
            </form>
        </div>
    </div>
</div>
</div>
<script th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/js/common.js}"></script>
<script th:src="@{/bootstrap/dist/js/bootstrap.min.js}"></script>
<script>

$(function () {
  $("#submit").click(function(){
  	submit();
  	return false;
  });
  
});

//校验密码
function checkLogin(){
	if($("input[name='password']").val() == ""){
		$("#error-msg").text("password cannot be empty");
		$("input[name='password']").focus();
		return false;
	}
	return true;
 }
//提交登录按钮
function submit(){
	if(checkLogin()){
  	$.ajax({
	type:"POST",
       url:_ctx+"/user/login",
       data:{username:$("input[name='username']").val(), password:$("input[name='password']").val(),time:new Date().getTime()},
       dataType:"json",
       cache:false,
       success: function(data){
      	 if("success" == data.status){
      		 window.location.href=_ctx+"/index";
      	 }else{
      		 $("#error-msg").html(data.msg);
      	 }
       }
	})
	}
}
//回车键监听
document.onkeydown = function (e) {
    if (!e) e = window.event;
    if ((e.keyCode || e.which) == 13) {
    	$("input[name='password']").focus();
    }
}
</script>
</body>
</html>